package com.ruoyi.ewem.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ewem.domain.*;
import com.ruoyi.ewem.domain.Process;
import com.ruoyi.ewem.mapper.ProcessMapper;
import com.ruoyi.ewem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品Controller
 *
 * @author ruoyi
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/process")
public class ProcessController extends BaseController
{
    @Autowired
    private ProcessService processService;

    @Autowired
    private ProductService productService;

    @Autowired
    private FirmService firmService;

    @Autowired
    private QualityService qualityService;

    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private  OutinfoService outinfoService;

    @Autowired
    private IninfoService ininfoService;

    /**
     * 查询产品列表
     */
    @RequiresPermissions("ewem:process:list")
    @GetMapping("/list")
    public TableDataInfo list(Process process) {
        startPage();
        List<Process> list = processService.selectProcessList(process);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("ewem:process:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Process process) {
        List<Process> list = processService.selectProcessList(process);
        ExcelUtil<Process> util = new ExcelUtil<Process>(Process.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @RequiresPermissions("ewem:process:query")
    @GetMapping(value = "/{processId}")
    public AjaxResult getInfo(@PathVariable("processId") Long processId) {
        return success(processService.getById(processId));
    }

    /**
     * 新增产品
     */
    @RequiresPermissions("ewem:process:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Process process) {

        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getProductId, process.getProductId());
        Product product = productService.getOne(productLambdaQueryWrapper);

        process.setBatchNum(product.getBatchNum());
        return toAjax(processService.save(process));
    }

    /**
     * 修改产品
     */
    @RequiresPermissions("ewem:process:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Process process) {
        return toAjax(processService.updateById(process));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("ewem:process:remove")
    @DeleteMapping("/{processIds}")
    public AjaxResult remove(@PathVariable Long[] processIds) {
        processService.deleteDictTypeByIds(processIds);
        return success();
    }

    @PostMapping("typelist1")
    public AjaxResult typelist1() {
        List<Quality> list = qualityService.list();
        String batchNum="";
        ArrayList<Product> productLists = new ArrayList<>();
        for (Quality quality : list) {
            batchNum=quality.getBatchNum();
            List<Product> productList=productService.selectList(batchNum);
            productLists.addAll(productList);
        }

        return AjaxResult.success(productLists);
    }

    @PostMapping("typelist2")
public AjaxResult typelist2(){
    return AjaxResult.success(firmService.list());
}



@PostMapping("typelist3")
public AjaxResult typelist3(){
    return AjaxResult.success(qualityService.list());
}

    @PostMapping("typelist4")
    public AjaxResult typelist4(){
        return AjaxResult.success(productService.list());
    }

    @GetMapping("list2")
    public TableDataInfo list2(){
      return getDataTable(processService.list());
    }

    @GetMapping("firmList")
    public TableDataInfo firmList(){
        return getDataTable(firmService.list());
    }

    @RequestMapping("listAll")
    public List<HashMap<String,Object>> listAll(){
        List<HashMap<String,Object>>  list = processMapper.selectByQuery();
        System.out.println(list);
        return list;
    }


    @GetMapping("qualityList")
    public TableDataInfo qualityList(){
        return getDataTable(qualityService.list());
    }


    @GetMapping("wareHouseList")
    public TableDataInfo wareHouseList(){
        return getDataTable(warehouseService.list());
    }

    @GetMapping("/detailList")
    public Map<String,Object> detailList(String batchNum){
        HashMap<String, Object> map = new HashMap<>();

        Process  process =  processService.selectByquery(batchNum);

        Firm firm =  firmService.selectByquery(batchNum);

        Quality quality = qualityService.selectByquery(batchNum);

        List<Outinfo> outinfo = outinfoService.selectByquery(batchNum);

        List<Ininfo> ininfo = ininfoService.selectByquery(batchNum);

        map.put("process",process);
        map.put("quality",quality);
        map.put("firm",firm);
        map.put("outing",outinfo);
        map.put("info",ininfo);
        System.err.println(map);
        return map;



    }

}





